package src.德鲁伊数据库连接池;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

public class DruidDemo {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/德鲁伊数据库连接池/DruidJdbc.properties");
        properties.load(fileInputStream);

        //使用德鲁伊数据连接
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //连接数据库
        Connection connection = dataSource.getConnection();
        // //创建sql语句
        String sql="select * from tbl_lixue";
        //预编译
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();

        //创建一个集合储存对象
        ArrayList<Brand> brands = new ArrayList<>();
        while(resultSet.next()){
            Brand brand = new Brand();
            brand.setId(resultSet.getInt("id"));
            brand.setUsername(resultSet.getString("username"));
            brand.setPassword(resultSet.getString("password"));
            brands.add(brand);
        }
        System.out.println("brands = " + brands);
    }
}
